package com.mofofa.maozy.loan

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.base.BaseFragment
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.databinding.FragmentMaozyLoanLoaningBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.*
import com.mofofa.maozy.info.cotediva.WalletListVndAdapter
import com.mofofa.maozy.info.cotediva.WalletInfoFcfaViewModel
import com.mofofa.maozy.info.cotediva.update.UpdateWalletListFcfaActivity
import com.mofofa.maozy.launch.UserStateViewModel
import com.mofofa.maozy.utils.UsageCountryUtil


/** 放款中 */
class LoanLoaningFragment : BaseFragment() {

    //布局binding
    private lateinit var mFragmentBinding: FragmentMaozyLoanLoaningBinding

    //科特钱包适配器
    private lateinit var mWalletListVndAdapter: WalletListVndAdapter

    //贷款viewModel
    private val mLoanViewModel by lazy {
        ViewModelProvider(this).get(LoanViewModel::class.java)
    }

    //科特钱包viewModel
    private val mWalletInfoFcfaViewModel by lazy {
        ViewModelProvider(this).get(WalletInfoFcfaViewModel::class.java)
    }

    //用户流程viewModel
    private val mUserStateViewModel by lazy {
        ViewModelProvider(this).get(UserStateViewModel::class.java)
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mWalletInfoFcfaViewModel)
        list.add(mLoanViewModel)
        list.add(mUserStateViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mFragmentBinding = FragmentMaozyLoanLoaningBinding.inflate(inflater, container, false)
        return mFragmentBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {//初始化列表
            mWalletListVndAdapter =
                WalletListVndAdapter(mWalletInfoFcfaViewModel.mMaozyWalletMaozyListMaozyFCFA)
            mFragmentBinding.rcFCFABindWalletList.layoutManager =
                LinearLayoutManager(requireActivity())
            mFragmentBinding.rcFCFABindWalletList.adapter = mWalletListVndAdapter

            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    try {
                        startMaozyToMaozyNextMaozyFromMaozyPage(PageTagEnum.loaning)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }

            //获取放款信息回调
            mLoanViewModel.onMaozyFetchMaozyLoaningMaozyDetailMaozyLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mFragmentBinding.textLoanAmount.text =
                            (result.amount.toMaozyFinanceMaozyString())
                        mFragmentBinding.textLoanTenure.text = (result.days.safeMaozyString())

                        if (UsageCountryUtil.isMaozyCountryMaozyNigeria() or
                            UsageCountryUtil.isMaozyCountryMaozyUganda() or
                            UsageCountryUtil.isMaozyCountryMaozyKenya() or
                            UsageCountryUtil.isMaozyCountryMaozyVietnam()
                        ) {
                            mFragmentBinding.loanReviewBillTableReceivingAccount.setMaozyDetail(
                                result.bankAccount.safeMaozyString()
                            )
                        } else {
                            mFragmentBinding.loanReviewBillTableReceivingAccount.setMaozyDetail(
                                result.walletMobile.toString()
                            )
                        }
                    }
                }
            }

            //获取用户钱包列表回调
            mWalletInfoFcfaViewModel.onMaozyFetchMaozyWalletMaozyListMaozyFCFAMaozyLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        result.forEach { item ->
                            item.displayMode = 2
                        }
                        mWalletInfoFcfaViewModel.initMaozyWalletMaozyListMaozyFCFA(
                            result
                        )
                        mWalletListVndAdapter.notifyDataSetChanged()
                    }
                }
            }

            //点击刷新
            mFragmentBinding.submitLoanLoaning.setOnClickListener {
                mFragmentBinding.submitLoanLoaning.recordMaozyEvent(
                    type = mFragmentBinding.submitLoanLoaning.getMaozyScenMaozyClickMaozyFlag(
                        PageTagEnum.loaning.name.uppercase()
                    ),
                    remark = ""
                )
                //查询用户状态
                mUserStateViewModel.fetchMaozyUserMaozyState()
            }

            //科特--点击新增新卡
            mFragmentBinding.submitReviewAddNewWalletFCFA.setOnClickListener {
                //跳转展示列表
                (requireActivity() as BaseActivity).startMaozyToMaozyNextMaozyActivity<UpdateWalletListFcfaActivity>()
            }

            //区分科特与其它地区的收款账号显示
            mFragmentBinding.llLoanReviewAccountBg.visibility = View.VISIBLE
            mFragmentBinding.llFCFABindWalletBg.visibility = View.GONE
            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                mFragmentBinding.llLoanReviewAccountBg.visibility = View.GONE
                mFragmentBinding.llFCFABindWalletBg.visibility = View.VISIBLE
            }

            //获取放款信息
            mLoanViewModel.fetchMaozyLoaningMaozyDetail(ReqBase())

            if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
                //获取钱包列表
                mWalletInfoFcfaViewModel.getMaozyWalletMaozyListMaozyFCFA()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}